import { request } from "../../utlis/asyncWx";
// import regeneratorRuntime from "../../lib/runtime/runtime";
Page({
  data: {
    leftMenuList: [],
    rightContent: [],
    currentIndex: 0,
    scrollTop: 0
  },
  Cates: [],
  // 左侧点击事件
  handleItemTap(e) {
    const {
      index
    } = e.currentTarget.dataset
    // 精髓================================================
    let rightContent = this.Cates[index].children
    this.setData({
      currentIndex: index,
      rightContent,
      scrollTop: 0
    })
  },
  async getCates() {
    const {
      data: res
    } = await request({
      url: 'categories'
    })
    console.log('请求分类数据', res)
    this.Cates = res.message
    // 写入缓存数据
    wx.setStorageSync('cates', {
      time: Date.now(),
      data: this.Cates
    })
    let leftMenuList = this.Cates.map(v => v.cat_name)
    this.setData({
      leftMenuList
    })
    let rightContent = this.Cates[0].children
    this.setData({
      rightContent
    })
  },
  onLoad(options) {
    // 读取缓存
    const Cates = wx.getStorageSync('cates')
    console.log(Cates)
    if (!Cates) {
      this.getCates();
    } else {
      if (Date.now() - Cates.time > 1000 * 10) {
        // 重新发请求
        this.getCates();
      } else {
        this.Cates = Cates.data
        this.setData({
          leftMenuList: this.Cates.map(v => v.cat_name),
          rightContent: this.Cates[0].children
        })
      }
    }
  }
})